home *** CD-ROM | disk | FTP | other *** search
/ Personal Computer World 2008 February / PCWFEB08.iso / Software / Freeware / Miro 1.0 / Miro_Installer.exe / xulrunner / chrome / toolkit.jar / content / global / viewSource.js < prev    next >
Encoding:
Text File  |  2005-06-01  |  16.4 KB  |  573 lines

  1. //@line 39 "/c/mozilla/toolkit/components/viewsource/content/viewSource.js"
  2.  
  3. const pageLoaderIface = Components.interfaces.nsIWebPageDescriptor;
  4. const nsISelectionPrivate = Components.interfaces.nsISelectionPrivate;
  5. const nsISelectionController = Components.interfaces.nsISelectionController;
  6. var gBrowser = null;
  7. var gViewSourceBundle = null;
  8. var gPrefs = null;
  9.  
  10. var gLastLineFound = '';
  11. var gGoToLine = 0;
  12.  
  13. try {
  14.   var prefService = Components.classes["@mozilla.org/preferences-service;1"]
  15.                               .getService(Components.interfaces.nsIPrefService);
  16.   gPrefs = prefService.getBranch(null);
  17. } catch (ex) {
  18. }
  19.  
  20. var gSelectionListener = {
  21.   timeout: 0,
  22.   notifySelectionChanged: function(doc, sel, reason)
  23.   {
  24.     // Coalesce notifications within 100ms intervals.
  25.     if (!this.timeout)
  26.       this.timeout = setTimeout(updateStatusBar, 100);
  27.   }
  28. }
  29.  
  30. function onLoadViewSource() 
  31. {
  32.   viewSource(window.arguments[0]);
  33.   document.commandDispatcher.focusedWindow = content;
  34. //@line 74 "/c/mozilla/toolkit/components/viewsource/content/viewSource.js"
  35. }
  36.  
  37. function onUnloadViewSource()
  38. {
  39. //@line 81 "/c/mozilla/toolkit/components/viewsource/content/viewSource.js"
  40. }
  41.  
  42. function getBrowser()
  43. {
  44.   if (!gBrowser)
  45.     gBrowser = document.getElementById("content");
  46.   return gBrowser;
  47. }
  48.  
  49. function getSelectionController()
  50. {
  51.   return getBrowser().docShell
  52.     .QueryInterface(Components.interfaces.nsIInterfaceRequestor)
  53.     .getInterface(Components.interfaces.nsISelectionDisplay)
  54.     .QueryInterface(nsISelectionController);
  55.  
  56. }
  57.  
  58. function getViewSourceBundle()
  59. {
  60.   if (!gViewSourceBundle)
  61.     gViewSourceBundle = document.getElementById("viewSourceBundle");
  62.   return gViewSourceBundle;
  63. }
  64.  
  65. function viewSource(url)
  66. {
  67.   if (!url)
  68.     return false; // throw Components.results.NS_ERROR_FAILURE;
  69.  
  70.   getBrowser().addEventListener("unload", onUnloadContent, true);
  71.   getBrowser().addEventListener("load", onLoadContent, true);
  72.  
  73.   var loadFromURL = true;
  74.   //
  75.   // Parse the 'arguments' supplied with the dialog.
  76.   //    arg[0] - URL string.
  77.   //    arg[1] - Charset value in the form 'charset=xxx'.
  78.   //    arg[2] - Page descriptor used to load content from the cache.
  79.   //    arg[3] - Line number to go to.
  80.   //
  81.   if ("arguments" in window) {
  82.     var arg;
  83.     //
  84.     // Set the charset of the viewsource window...
  85.     //
  86.     if (window.arguments.length >= 2) {
  87.       arg = window.arguments[1];
  88.  
  89.       try {
  90.         if (typeof(arg) == "string" && arg.indexOf('charset=') != -1) {
  91.           var arrayArgComponents = arg.split('=');
  92.           if (arrayArgComponents) {
  93.             //we should "inherit" the charset menu setting in a new window
  94.             getMarkupDocumentViewer().defaultCharacterSet = arrayArgComponents[1];
  95.           } 
  96.         }
  97.       } catch (ex) {
  98.         // Ignore the failure and keep processing arguments...
  99.       }
  100.     }
  101.     //
  102.     // Get any specified line to jump to.
  103.     //
  104.     if (window.arguments.length >= 4) {
  105.       arg = window.arguments[3];
  106.       gGoToLine = parseInt(arg);
  107.     }
  108.     //
  109.     // Use the page descriptor to load the content from the cache (if
  110.     // available).
  111.     //
  112.     if (window.arguments.length >= 3) {
  113.       arg = window.arguments[2];
  114.  
  115.       try {
  116.         if (typeof(arg) == "object" && arg != null) {
  117.           var PageLoader = getBrowser().webNavigation.QueryInterface(pageLoaderIface);
  118.  
  119.           //
  120.           // Load the page using the page descriptor rather than the URL.
  121.           // This allows the content to be fetched from the cache (if
  122.           // possible) rather than the network...
  123.           //
  124.           PageLoader.loadPage(arg, pageLoaderIface.DISPLAY_AS_SOURCE);
  125.           // The content was successfully loaded from the page cookie.
  126.           loadFromURL = false;
  127.         }
  128.       } catch(ex) {
  129.         // Ignore the failure.  The content will be loaded via the URL
  130.         // that was supplied in arg[0].
  131.       }
  132.     }
  133.   }
  134.  
  135.   if (loadFromURL) {
  136.     //
  137.     // Currently, an exception is thrown if the URL load fails...
  138.     //
  139.     var loadFlags = Components.interfaces.nsIWebNavigation.LOAD_FLAGS_NONE;
  140.     var viewSrcUrl = "view-source:" + url;
  141.     getBrowser().webNavigation.loadURI(viewSrcUrl, loadFlags, null, null, null);
  142.   }
  143.  
  144.   //check the view_source.wrap_long_lines pref and set the menuitem's checked attribute accordingly
  145.   if (gPrefs) {
  146.     try {
  147.       var wraplonglinesPrefValue = gPrefs.getBoolPref("view_source.wrap_long_lines");
  148.  
  149.       if (wraplonglinesPrefValue)
  150.         document.getElementById('menu_wrapLongLines').setAttribute("checked", "true");
  151.     } catch (ex) {
  152.     }
  153.     try {
  154.       document.getElementById("menu_highlightSyntax").setAttribute("checked", gPrefs.getBoolPref("view_source.syntax_highlight"));
  155.     } catch (ex) {
  156.     }
  157.   } else {
  158.     document.getElementById("menu_highlightSyntax").setAttribute("hidden", "true");
  159.   }
  160.  
  161.   window._content.focus();
  162.  
  163.   return true;
  164. }
  165.  
  166. function onLoadContent()
  167. {
  168.   //
  169.   // If the view source was opened with a "go to line" argument.
  170.   //
  171.   if (gGoToLine > 0) {
  172.     goToLine(gGoToLine);
  173.     gGoToLine = 0;
  174.   }
  175.   document.getElementById('cmd_goToLine').removeAttribute('disabled');
  176.  
  177.   // Register a listener so that we can show the caret position on the status bar.
  178.   window._content.getSelection()
  179.    .QueryInterface(nsISelectionPrivate)
  180.    .addSelectionListener(gSelectionListener);
  181. }
  182.  
  183. function onUnloadContent()
  184. {
  185.   //
  186.   // Disable "go to line" while reloading due to e.g. change of charset
  187.   // or toggling of syntax highlighting.
  188.   //
  189.   document.getElementById('cmd_goToLine').setAttribute('disabled', 'true');
  190. }
  191.  
  192. function ViewSourceClose()
  193. {
  194.   window.close();
  195. }
  196.  
  197. function ViewSourceReload()
  198. {
  199.   const webNavigation = getBrowser().webNavigation;
  200.   webNavigation.reload(webNavigation.LOAD_FLAGS_BYPASS_PROXY | webNavigation.LOAD_FLAGS_BYPASS_CACHE);
  201. }
  202.  
  203. // Strips the |view-source:| for editPage()
  204. function ViewSourceEditPage()
  205. {
  206.   editPage(window.content.location.href.substring(12), window, false);
  207. }
  208.  
  209. // Strips the |view-source:| for saveURL()
  210. function ViewSourceSavePage()
  211. {
  212.   saveURL(window.content.location.href.substring(12), null, "SaveLinkTitle");
  213. }
  214.  
  215. function onEnterPP()
  216. {
  217.   var toolbox = document.getElementById("viewSource-toolbox");
  218.   toolbox.hidden = true;
  219. }
  220.  
  221. function onExitPP()
  222. {
  223.   var toolbox = document.getElementById("viewSource-toolbox");
  224.   toolbox.hidden = false;
  225. }
  226.  
  227. function ViewSourceGoToLine()
  228. {
  229.   var promptService = Components.classes["@mozilla.org/embedcomp/prompt-service;1"]
  230.         .getService(Components.interfaces.nsIPromptService);
  231.   var viewSourceBundle = getViewSourceBundle();
  232.  
  233.   var input = {value:gLastLineFound};
  234.   for (;;) {
  235.     var ok = promptService.prompt(
  236.         window,
  237.         viewSourceBundle.getString("goToLineTitle"),
  238.         viewSourceBundle.getString("goToLineText"),
  239.         input,
  240.         null,
  241.         {value:0});
  242.  
  243.     if (!ok) return;
  244.  
  245.     var line = parseInt(input.value);
  246.  
  247.     if (!(line > 0)) {
  248.       promptService.alert(window,
  249.           viewSourceBundle.getString("invalidInputTitle"),
  250.           viewSourceBundle.getString("invalidInputText"));
  251.   
  252.       continue;
  253.     }
  254.  
  255.     var found = goToLine(line);
  256.  
  257.     if (found) {
  258.       break;
  259.     }
  260.  
  261.     promptService.alert(window,
  262.         viewSourceBundle.getString("outOfRangeTitle"),
  263.         viewSourceBundle.getString("outOfRangeText"));
  264.   }
  265. }
  266.  
  267. function goToLine(line)
  268. {
  269.   var viewsource = window._content.document.body;
  270.  
  271.   //
  272.   // The source document is made up of a number of pre elements with
  273.   // id attributes in the format <pre id="line123">, meaning that
  274.   // the first line in the pre element is number 123.
  275.   // Do binary search to find the pre element containing the line.
  276.   //
  277.   var pre;
  278.   for (var lbound = 0, ubound = viewsource.childNodes.length; ; ) {
  279.     var middle = (lbound + ubound) >> 1;
  280.     pre = viewsource.childNodes[middle];
  281.  
  282.     var firstLine = parseInt(pre.id.substring(4));
  283.  
  284.     if (lbound == ubound - 1) {
  285.       break;
  286.     }
  287.  
  288.     if (line >= firstLine) {
  289.       lbound = middle;
  290.     } else {
  291.       ubound = middle;
  292.     }
  293.   }
  294.  
  295.   var result = {};
  296.   var found = findLocation(pre, line, null, -1, false, result);
  297.  
  298.   if (!found) {
  299.     return false;
  300.   }
  301.  
  302.   var selection = window._content.getSelection();
  303.   selection.removeAllRanges();
  304.  
  305.   // In our case, the range's startOffset is after "\n" on the previous line.
  306.   // Tune the selection at the beginning of the next line and do some tweaking
  307.   // to position the focusNode and the caret at the beginning of the line.
  308.  
  309.   selection.QueryInterface(nsISelectionPrivate)
  310.     .interlinePosition = true;    
  311.  
  312.   selection.addRange(result.range);
  313.  
  314.   if (!selection.isCollapsed) {
  315.     selection.collapseToEnd();
  316.  
  317.     var offset = result.range.startOffset;
  318.     var node = result.range.startContainer;
  319.     if (offset < node.data.length) {
  320.       // The same text node spans across the "\n", just focus where we were.
  321.       selection.extend(node, offset);
  322.     }
  323.     else {
  324.       // There is another tag just after the "\n", hook there. We need
  325.       // to focus a safe point because there are edgy cases such as
  326.       // <span>...\n</span><span>...</span> vs.
  327.       // <span>...\n<span>...</span></span><span>...</span>
  328.       node = node.nextSibling ? node.nextSibling : node.parentNode.nextSibling;
  329.       selection.extend(node, 0);
  330.     }
  331.   }
  332.  
  333.   var selCon = getSelectionController();
  334.   selCon.setDisplaySelection(nsISelectionController.SELECTION_ON);
  335.   selCon.setCaretEnabled(true);
  336.   selCon.setCaretVisibilityDuringSelection(true);
  337.  
  338.   // Scroll the beginning of the line into view.
  339.   selCon.scrollSelectionIntoView(
  340.     nsISelectionController.SELECTION_NORMAL,
  341.     nsISelectionController.SELECTION_FOCUS_REGION,
  342.     true);
  343.  
  344.   gLastLineFound = line;
  345.  
  346.   //pch: don't update the status bar for now
  347.   //document.getElementById("statusbar-line-col").label = getViewSourceBundle()
  348.   //    .getFormattedString("statusBarLineCol", [line, 1]);
  349.  
  350.   return true;
  351. }
  352.  
  353. function updateStatusBar()
  354. {
  355.   // Reset the coalesce flag.
  356.   gSelectionListener.timeout = 0;
  357.  
  358.   var statusBarField = document.getElementById("statusbar-line-col");
  359.  
  360.   var selection = window._content.getSelection();
  361.   if (!selection.focusNode) {
  362.     statusBarField.label = '';
  363.     return;
  364.   }
  365.   if (selection.focusNode.nodeType != Node.TEXT_NODE) {
  366.     return;
  367.   }
  368.  
  369.   var selCon = getSelectionController();
  370.   selCon.setDisplaySelection(nsISelectionController.SELECTION_ON);
  371.   selCon.setCaretEnabled(true);
  372.   selCon.setCaretVisibilityDuringSelection(true);
  373.  
  374.   var interlinePosition = selection
  375.       .QueryInterface(nsISelectionPrivate).interlinePosition;
  376.  
  377.   var result = {};
  378.   findLocation(null, -1, 
  379.       selection.focusNode, selection.focusOffset, interlinePosition, result);
  380.  
  381.   //pch no status bar for now
  382.   return;
  383.   statusBarField.label = getViewSourceBundle()
  384.       .getFormattedString("statusBarLineCol", [result.line, result.col]);
  385. }
  386.  
  387. //
  388. // Loops through the text lines in the pre element. The arguments are either
  389. // (pre, line) or (node, offset, interlinePosition). result is an out
  390. // argument. If (pre, line) are specified (and node == null), result.range is
  391. // a range spanning the specified line. If the (node, offset,
  392. // interlinePosition) are specified, result.line and result.col are the line
  393. // and column number of the specified offset in the specified node relative to
  394. // the whole file.
  395. //
  396. function findLocation(pre, line, node, offset, interlinePosition, result)
  397. {
  398.   if (node && !pre) {
  399.     //
  400.     // Look upwards to find the current pre element.
  401.     //
  402.     for (pre = node;
  403.          pre.nodeName != "PRE";
  404.          pre = pre.parentNode);
  405.   }
  406.  
  407.   //
  408.   // The source document is made up of a number of pre elements with
  409.   // id attributes in the format <pre id="line123">, meaning that
  410.   // the first line in the pre element is number 123.
  411.   //
  412.   var curLine = parseInt(pre.id.substring(4));
  413.  
  414.   //
  415.   // Walk through each of the text nodes and count newlines.
  416.   //
  417.   var treewalker = window._content.document
  418.       .createTreeWalker(pre, NodeFilter.SHOW_TEXT, null, false);
  419.  
  420.   //
  421.   // The column number of the first character in the current text node.
  422.   //
  423.   var firstCol = 1;
  424.  
  425.   var found = false;
  426.   for (var textNode = treewalker.firstChild();
  427.        textNode && !found;
  428.        textNode = treewalker.nextNode()) {
  429.  
  430.     //
  431.     // \r is not a valid character in the DOM, so we only check for \n.
  432.     //
  433.     var lineArray = textNode.data.split(/\n/);
  434.     var lastLineInNode = curLine + lineArray.length - 1;
  435.  
  436.     //
  437.     // Check if we can skip the text node without further inspection.
  438.     //
  439.     if (node ? (textNode != node) : (lastLineInNode < line)) {
  440.       if (lineArray.length > 1) {
  441.         firstCol = 1;
  442.       }
  443.       firstCol += lineArray[lineArray.length - 1].length;
  444.       curLine = lastLineInNode;
  445.       continue;
  446.     }
  447.  
  448.     //
  449.     // curPos is the offset within the current text node of the first
  450.     // character in the current line.
  451.     //
  452.     for (var i = 0, curPos = 0;
  453.          i < lineArray.length;
  454.          curPos += lineArray[i++].length + 1) {
  455.  
  456.       if (i > 0) {
  457.         curLine++;
  458.       }
  459.  
  460.       if (node) {
  461.         if (offset >= curPos && offset <= curPos + lineArray[i].length) {
  462.           //
  463.           // If we are right after the \n of a line and interlinePosition is
  464.           // false, the caret looks as if it were at the end of the previous
  465.           // line, so we display that line and column instead.
  466.           //
  467.           if (i > 0 && offset == curPos && !interlinePosition) {
  468.             result.line = curLine - 1;
  469.             var prevPos = curPos - lineArray[i - 1].length;
  470.             result.col = (i == 1 ? firstCol : 1) + offset - prevPos;
  471.  
  472.           } else {
  473.             result.line = curLine;
  474.             result.col = (i == 0 ? firstCol : 1) + offset - curPos;
  475.           }
  476.           found = true;
  477.  
  478.           break;
  479.         }
  480.  
  481.       } else {
  482.         if (curLine == line && !("range" in result)) {
  483.           result.range = document.createRange();
  484.           result.range.setStart(textNode, curPos);
  485.  
  486.           //
  487.           // This will always be overridden later, except when we look for
  488.           // the very last line in the file (this is the only line that does
  489.           // not end with \n).
  490.           //
  491.           result.range.setEndAfter(pre.lastChild);
  492.  
  493.         } else if (curLine == line + 1) {
  494.           result.range.setEnd(textNode, curPos - 1);
  495.           found = true;
  496.           break;
  497.         }
  498.       }
  499.     }
  500.   }
  501.  
  502.   return found || ("range" in result);
  503. }
  504.  
  505. //function to toggle long-line wrapping and set the view_source.wrap_long_lines 
  506. //pref to persist the last state
  507. function wrapLongLines()
  508. {
  509.   var myWrap = window._content.document.body;
  510.  
  511.   if (myWrap.className == '')
  512.     myWrap.className = 'wrap';
  513.   else myWrap.className = '';
  514.  
  515.   //since multiple viewsource windows are possible, another window could have 
  516.   //affected the pref, so instead of determining the new pref value via the current
  517.   //pref value, we use myWrap.className  
  518.   if (gPrefs){
  519.     try {
  520.       if (myWrap.className == '') {
  521.         gPrefs.setBoolPref("view_source.wrap_long_lines", false);
  522.       }
  523.       else {
  524.         gPrefs.setBoolPref("view_source.wrap_long_lines", true);
  525.       }
  526.     } catch (ex) {
  527.     }
  528.   }
  529. }
  530.  
  531. //function to toggle syntax highlighting and set the view_source.syntax_highlight
  532. //pref to persist the last state
  533. function highlightSyntax()
  534. {
  535.   var highlightSyntaxMenu = document.getElementById("menu_highlightSyntax");
  536.   var highlightSyntax = (highlightSyntaxMenu.getAttribute("checked") == "true");
  537.   gPrefs.setBoolPref("view_source.syntax_highlight", highlightSyntax);
  538.  
  539.   var PageLoader = getBrowser().webNavigation.QueryInterface(pageLoaderIface);
  540.   PageLoader.loadPage(PageLoader.currentDescriptor, pageLoaderIface.DISPLAY_NORMAL);
  541. }
  542.  
  543. // Fix for bug 136322: this function overrides the function in
  544. // browser.js to call PageLoader.loadPage() instead of BrowserReloadWithFlags()
  545. function BrowserSetForcedCharacterSet(aCharset)
  546. {
  547.   var docCharset = getBrowser().docShell.QueryInterface(
  548.                             Components.interfaces.nsIDocCharset);
  549.   docCharset.charset = aCharset;
  550.   var PageLoader = getBrowser().webNavigation.QueryInterface(pageLoaderIface);
  551.   PageLoader.loadPage(PageLoader.currentDescriptor, pageLoaderIface.DISPLAY_NORMAL);
  552. }
  553.  
  554. // fix for bug #229503
  555. // we need to define BrowserSetForcedDetector() so that we can
  556. // change auto-detect options in the "View | Character Encoding" menu.
  557. // As with BrowserSetForcedCharacterSet(), call PageLoader.loadPage() 
  558. // instead of BrowserReloadWithFlags()
  559. function BrowserSetForcedDetector(doReload)
  560. {
  561.   getBrowser().documentCharsetInfo.forcedDetector = true; 
  562.   if (doReload)
  563.   {
  564.     var PageLoader = getBrowser().webNavigation.QueryInterface(pageLoaderIface);
  565.     PageLoader.loadPage(PageLoader.currentDescriptor, pageLoaderIface.DISPLAY_NORMAL);
  566.   }
  567. }
  568.  
  569. function getMarkupDocumentViewer()
  570. {
  571.   return gBrowser.markupDocumentViewer;
  572. }
  573.